<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Dsj as Dsjs;
use app\admin\model\ArctypeMod;

class Dsj extends Common
{
    private $cModel;   //当前控制器关联模型

    public function _initialize()
    {
        parent::_initialize();
        $this->cModel = new Dsjs();  //别名：避免与控制名冲突
    }

    public function index()
    {
        $where = [];
        if (input('get.search')){
            $where['title'] = ['like', '%'.input('get.search').'%'];//通过nme进行模糊查询
        }
        if (input('get._sort')){//判断是否有值
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'id asc';
        }
        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());

        $this->assign('dataList', $dataList);
        return $this->fetch();
    }

    public function create()
    {
        if (request()->isPost()){
            $data = input('post.');//POST传值
            $result = $this->cModel->validate(CONTROLLER_NAME.'.add')->allowField(true)->save($data);
            //allowField两个参数  过滤非数据表中的字段 true过滤 flase不过滤
            //validate验证器
            cache('DB_TREE_ARETYPE', null);   //删除栏目缓存
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());//getError 是用来获取错误信息
            }
        }else{

            $this->assign('arctypeList', $arctypeList);

            $amModel = new ArctypeMod();
            $modList = $amModel->where(['status' => 1])->order('sorts ASC,id ASC')->select();
            $this->assign('modList', $modList);
            return $this->fetch('edit');
        }
    }

    public function edit($id)
    {
        if (request()->isPost()){
            $data = input('post.');
            if (count($data) == 2){
                foreach ($data as $k =>$v){
                    $fv = $k!='id' ? $k : '';//三目运算符
                }
                $result = $this->cModel->validate(CONTROLLER_NAME.'.'.$fv)->allowField(true)->save($data, $data['id']);
                //allowField 两个参数 过滤非数据表中的字段 true（过滤） flase（不过滤）
                //save thinkphp中的助手函数，主要用来更新数据
            }else{
                $result = $this->cModel->validate(CONTROLLER_NAME.'.edit')->allowField(true)->save($data, $data['id']);
            }
            cache('DB_TREE_ARETYPE', null);   //删除栏目缓存
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));//ajaxReturn三个参数 返回数据 提示信息 操作状态
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{

            $data = $this->cModel->get($id);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    public function delete()
    {
        if (request()->isPost()){//是否是POST传值
            $id = input('id');
            if (isset($id) && !empty($id)){//isset判断是否存在
                $id_arr = explode(',', $id);
                $where = [ 'id' => ['in', $id_arr] ];
                $result = $this->cModel->where($where)->delete();
                cache('DB_TREE_ARETYPE', null);   //删除栏目缓存
                //cache=缓存 三个参数 缓存名称 缓存的值 缓存有效期 0表示永久缓存
                if ($result){
                    return ajaxReturn(lang('action_success'), url('index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            }
        }
    }
}